#define LG_CORE

#include "lg_window.h"

static struct{
	LGConfig* config;
	LGWindowBackend* backend;
	LGWindow* root;
}ctx = {0};


LG_API int
lgInitWindowContext(LGConfig* config, LGWindowBackend* backend){
	ctx.config = config;
	ctx.backend = backend;
	return 0;
}

LG_API int
lgExitWindowContext() {
	return 0;
}

LG_API LGWindow*
lgOpenWindow(const char* title, LGWindowStyle* style, void* ud) {
	size_t size = ctx.backend->sizeofWindow();
	LGWindow* window = malloc(size);
	window->style = *style;
	window->ud = ud;
	window->title[0] = '\0';
	window->next = NULL;
	window->width = style->width;
	window->height = style->height;
	window->onEvt = style->onEvt;
	ctx.backend->openWindow(window);
	if (ctx.root) {
		ctx.root->next = window;
	}
	else {
		ctx.root = window;
	}
	return window;
}

LG_API int
lgCloseWindow(LGWindow* window) {
	return ctx.backend->closeWindow(window);
}

LG_API void
lgGetWindowSize(LGWindow* window, unsigned* w, unsigned* h) {
	*w = window->width;
	*h = window->height;
}

LG_API void
lgRenderWindow(LGWindow* window, LGCanvas* canvas) {
	ctx.backend->renderWindow(window, canvas);
}


LG_API void
lgOnWindowEvt(LGWindow* window, void (*onEvt)(LGWindow*, LGWindowEvt)) {
	window->onEvt = onEvt;
}